package com.asia5b.a5mall.order.activity

import android.content.Context
import android.content.Intent
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.event.PlaceSelectEvent
import com.asia5b.a5mall.middleware.model.bean.SelectText
import com.asia5b.a5mall.middleware.model.bean.SelectType
import com.asia5b.a5mall.middleware.utils.getEventBus

import com.asia5b.a5mall.middleware.utils.pop
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.adapter.PlaceSelectAdapter
import com.asia5b.a5mall.order.databinding.ActivityPlaceselectBinding
import com.google.android.flexbox.FlexboxLayoutManager
import kotlinx.android.synthetic.main.activity_placeselect.*
import java.io.Serializable

/**
 * Created by zhangyuncai on 2020/5/7.
 * 仓库选择 支付类型
 */
class PlaceSelectActivity : SimpleActivity<SimpleViewModel, ActivityPlaceselectBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivityPlaceselectBinding {
        return ActivityPlaceselectBinding.inflate(layoutInflater)
    }
    companion object {
        fun startPlaceSelectActivity(context: Context, type: SelectType, datas: Serializable, selectText: String) {
            val intent = Intent(context, PlaceSelectActivity::class.java)
            intent.putExtra("type", type)
            intent.putExtra("datas", datas)
            intent.putExtra("selectText", selectText)
            context.startActivity(intent)
        }
    }

    override var sIsDark = true
    val headBar by lazy { headbar_placeselect }
    val mRecyclerView by lazy { rv_placegoodlist }
    val mDatas = arrayListOf<SelectText>()
    val mAdapter by lazy { PlaceSelectAdapter(mDatas) }
    var type = SelectType.intertrans
    var selectText: String? = null

    override fun initView() {
        if (intent.hasExtra("datas")) {
            mDatas.addAll(intent!!.getSerializableExtra("datas") as ArrayList<SelectText>)
        }
        if (intent!!.hasExtra("type")) {
            type = intent!!.getSerializableExtra("type") as SelectType
        }
        if (intent!!.hasExtra("selectText")) {
            selectText = intent!!.getStringExtra("selectText")
        }
        val tv_title_titletxt = headBar.tv_title_titletxt
        when (type) {
            SelectType.intertrans -> {
                tv_title_titletxt.text = getString(R.string.国际运输方式)
                tv_placeselect_type.text = getString(R.string.国际运输方式)
            }
            SelectType.localtrans -> {
                tv_title_titletxt.text = getString(R.string.派送方式)
                tv_placeselect_type.text = getString(R.string.派送方式)
            }
            SelectType.house -> {
                tv_title_titletxt.text = getString(R.string.选中仓库)
                tv_placeselect_type.text = getString(R.string.选中仓库)
            }
            SelectType.pay -> {
                tv_title_titletxt.text = getString(R.string.支付类型)
                tv_placeselect_type.text = getString(R.string.支付类型)
            }
        }

        mAdapter.selectText = selectText
        mRecyclerView.run {
            layoutManager = FlexboxLayoutManager(mActivity)!!
            adapter = mAdapter
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val transModel = mAdapter.getItem(position) as SelectText
            getEventBus().post(PlaceSelectEvent(type, transModel))
            pop()
        }
    }

    override fun initEvent() {
    }

    override fun initData() {
    }
}